package com.midea.ai.air.ui.more;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.ui.user.LoginActivity;
import com.midea.ai.air.view.MyTitleBar;

/**
 * Created by hww on 2016/3/24.
 * 更多-设置-修改密码
 */
public class MoreSettingChangePassword extends BaseActivity implements View.OnClickListener {

    private ImageView old_img, new_img;
    private EditText old_ed, new_ed;
    private TextView old_error,new_error;
    private TextView actionbar_right_tv;
    private Dialog change_pw_Dialog;
    private Dialog pw_Success_Dialog;
    private static final int SUCCESS_CHANGE_PASSWORD = 0X110;
    private static final int SUCCESS_CHANGE_PASSWORD_TWO = 0X111;


    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SUCCESS_CHANGE_PASSWORD:
                    change_pw_Dialog.dismiss();
                    pw_Success_Dialog.show();
                    handler.sendEmptyMessageDelayed(SUCCESS_CHANGE_PASSWORD_TWO,300);
                    break;
                case SUCCESS_CHANGE_PASSWORD_TWO:
                    pw_Success_Dialog.dismiss();
                    MoreSettingChangePassword.this.finish();
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moresettingchangepassword);

        init();
    }

    MyTitleBar myTitleBar;
    RelativeLayout rl;
    private void init() {
//        rl = (RelativeLayout)this.findViewById(R.id.action_titler_bar);
//        ViewGroup.LayoutParams LP = rl.getLayoutParams();
//        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
//        rl.setLayoutParams(LP);
        myTitleBar = new MyTitleBar(this);
        myTitleBar.setActionBar_center(R.string.more_setting_change_pw);
        myTitleBar.setActioBar_left(false, 0, R.string.dialog_cancel);
        myTitleBar.setActionBar_right(true, R.string.action_save);

        change_pw_Dialog = LoginActivity.createLoadingDialog(this,R.string.dialog_change_password);
        pw_Success_Dialog = createLoadingDialog(this, R.string.dialog_change_password_success, R.drawable.register_dialog_success);

        old_ed = (EditText) findViewById(R.id.more_setting_changePassword_old_pw_ed);
        new_ed = (EditText) findViewById(R.id.more_setting_changePassword_new_pw_ed);
        old_img = (ImageView) findViewById(R.id.more_setting_changePassword_old_pw_img);
        new_img = (ImageView) findViewById(R.id.more_setting_changePassword_new_pw_img);
        old_error = (TextView)findViewById(R.id.more_setting_changePassword_old_pw_error_tv);
        new_error = (TextView)findViewById(R.id.more_setting_changePassword_new_pw_error_tv);
        actionbar_right_tv = (TextView) findViewById(R.id.actionbar_tv_right);
        actionbar_right_tv.setTextColor(getResources().getColor(R.color.electric_histogram));
        actionbar_right_tv.setAlpha(0.25f);
        old_ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        new_ed.setTransformationMethod(HideReturnsTransformationMethod.getInstance());



        //忘记密码下一步按钮激活
        new_ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(old_ed.getText())) {
                    if (count < 0) {
                        actionbar_right_tv.setAlpha(0.25f);
                    } else {
                        actionbar_right_tv.setAlpha(1f);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //输入框焦点，判断是否显示图标
        old_ed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    old_img.setVisibility(View.VISIBLE);
                } else {
                    old_img.setVisibility(View.INVISIBLE);
                }
            }
        });

        new_ed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    new_img.setVisibility(View.VISIBLE);
                } else {
                    new_img.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.actionbar_left_relativeLayout:
                //取消
                finish();
                break;
            case R.id.actionbar_tv_right:
                //保存

                save_change_password();
                break;
            default:
                break;
        }
    }


    private void save_change_password(){
        String old_password = old_ed.getText().toString().trim();
        String new_password = new_ed.getText().toString().trim();

        if(TextUtils.isEmpty(old_password)){
            old_error.setText(R.string.log_in_password_empty);
            return;
        }
        if(TextUtils.isEmpty(new_password)){
            new_error.setText(R.string.log_in_password_empty);
            return;
        }
        //旧密码错误判断
//        if(old_password.equals()){
//            old_error.setText(R.string.more_setting_change_old_pw_error_hint);
//            return;
//        }

        if (new_password.length() < 6 || new_password.length() > 16) {
            new_error.setText(R.string.register_password_error);
            return;
        }

        if (LoginActivity.isSpecialCharacter(new_password)) {
            new_error.setText(R.string.register_password_error);
            return;
        }

        change_pw_Dialog.show();
        handler.sendEmptyMessageDelayed(SUCCESS_CHANGE_PASSWORD,1000);
    }




    public static Dialog createLoadingDialog(Context context, int id,int R_drawable) {
        String msg = context.getResources().getString(id);
        LoginActivity.mLog("myDialog_msg = " + msg);
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.myloadingdialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        ImageView load_img = (ImageView)v.findViewById(R.id.loadingImg);
        TextView tipTextView = (TextView) v.findViewById(R.id.loadingmsg);// 提示文字
        load_img.setImageResource(R_drawable);
        tipTextView.setText(msg);// 设置加载信息
        Dialog loadingDialog = new Dialog(context, R.style.my_loading_dialog);// 创建自定义样式dialog
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));// 设置布局
        return loadingDialog;
    }


}
